#include "msglist.h"
msglist::msglist () {
	first = 0;
	last = 0;
	last5 = 0;
	num = 0;
}
msglist::~msglist () {
	msg*it = first;
	while (it) {
		msg*sw = it->next;
		delete it;
		it = sw;
	}
	first = 0;
	last5 = 0;
	num = 0;
}
void msglist::add (std::string&text0) {
	SYSTEMTIME st;
	GetSystemTime (&st);

	msg*newMsg = new msg;
	newMsg->time = std::time (0) * 1000 + st.wMilliseconds;
	newMsg->text = text0;
	newMsg->next = 0;
	if (first) {
		last->next = newMsg;
		last = last->next;
	}
	else
		last = first = newMsg;
	num++;
	last5 = num > 5 ? last5->next : first;
}
void msglist::all (std::stringstream&stream0) {
	msg*it = first;
	while (it) {
		stream0 << it->text << "\r\n";
		it = it->next;
	}
}
void msglist::from (unsigned int from, std::stringstream&stream0) {
	msg*it;
	if (from == -1)
		it = last5;
	else {
		unsigned int at = 0;
		if (from > num - 5) {
			at = num - 5;
			it = last5;
		}
		else
			it = first;
		while (at < from) {
			it = it->next;
			at++;
		}
	}
	while (it) {
		stream0 << std::hex << std::setw (16) << std::setfill ('0') << it->time << " " << it->text << "\r\n";
		it = it->next;
	}
	stream0 << std::hex << std::setw (8) << std::setfill ('0') << num/* << "\r\n"*/;
}